import { Component, OnInit } from '@angular/core';
import { HttpRequest } from "../../../../service/httpRequest";
import { Router } from '@angular/router';
@Component({
  selector: 'app-order-new',
  templateUrl: './order-new.component.html',
  styleUrls: ['./order-new.component.css',
    '../../../../../assets/css/timeline.css']
})
export class OrderNewComponent implements OnInit {

  time;
  groupData = [];
  deviceData = [];
  data:any;
  username:string = '';
  group:string = '';
  device:string = '';
  des:string = '';
  constructor(private _router: Router,private httpRequest:HttpRequest) { }

  ngOnInit() {
    this.username = JSON.parse(localStorage.getItem('account')).username;
    this.time = this.httpRequest.gettoday();
    this.getGroups();
  }

  getGroups(){
    this.httpRequest.sendGet('device/group/list').subscribe(res => {
      this.groupData = res.result;
      for(let i=0;i<res.result.length;i++){
        this.groupData[i].ds = res.result[i].id + "," + res.result[i].lxr + "," + res.result[i].lxdh
      }
    },err => console.log(err));
  }

  getDevice(){
    this.deviceData = [];
    this.data = {};
    this.device = '';
    //console.log(this.group);
    if( this.group != ''){
      this.httpRequest.sendGet('devices/bygroup',{
        group:this.group.split(',')[0]
      }).subscribe(res => {
        //console.log(res);
        this.deviceData = res.result;
      },err => console.log(err));
    }
  }

  getData(){
    this.data = {};
    if( this.device != ''){
      this.httpRequest.sendGet('devices',{
        id:this.device
      }).subscribe(res => {
        //console.log(res);
        this.data = res.result;
      },err => console.log(err));
    }
  }

  save(){
    this.httpRequest.sendPost('guzhang',{
      group_id:this.group.split(',')[0],
      device_id:this.device,
      description:this.des
    }).subscribe(res => {
      alert("新增工单成功！");
      this._router.navigateByUrl('page/repair/order');
    },err => console.log(err));

  }

  cancel(){
    this._router.navigateByUrl('page/repair/order');
  }
}
